<template>
	<view class="pigeonhole">
		<!-- 归档 -->
		<navBar :title="data.bizProjectName||''" :title2="(data.sectionName||'')+'、'+(data.siteName||'')"></navBar>

		<view class="content">
			<view class="box">
				<view class="item">
					<view class="name">业务类型</view>
					<!-- <view class="value"></view> -->
					<uni-data-picker :localdata="businessTypeList" :map="mapObj" v-model="businessTypeId"
						placeholder="请选择业务类型" :clear-icon="false" popup-title="请选择业务类型" @change="onchange">
					</uni-data-picker>
				</view>
				<view class="item">
					<view class="name">险情原因</view>
					<view class="form-item form-item-description">
                        <u-textarea v-model="dangerText" placeholder="请输入内容" confirmType="done" @blur="dangerTextBlur"></u-textarea>
						<record class="form-item-icon form-item-icon-voice" @setText="setdangerText"></record>
						<image mode="scaleToFill" class="form-item-icon form-item-icon-clear"
							   src="/static/image/login/del1.png" @click="dangerText=''"></image>
					</view>
				</view>
				<view class="item">
					<view class="name">经验总结</view>
					<view class="form-item form-item-description">
                        <u-textarea v-model="summaryText" placeholder="请输入内容" confirmType="done" @blur="summaryTextBlur"></u-textarea>
						<record class="form-item-icon form-item-icon-voice" @setText="setsummaryText"></record>
						<image mode="scaleToFill" class="form-item-icon form-item-icon-clear"
							   src="/static/image/login/del1.png" @click="summaryText=''"></image>
					</view>
				</view>
				<!-- <view class="upload">
					上传工作量清单
				</view>

				<u-upload :fileList="fileList" name="file" @afterRead="afterRead" @delete="deleteFile"
					multiple :maxCount="9"  accept="file"></u-upload> -->
			</view>
		</view>

		<view class="fixedButton">
			<u-button type="primary" :loading="loading" text="提交" @click="submit"></u-button>
		</view>
	</view>
</template>

<script>
	import {
		getEventSummarySave,
		getEventSave
	} from "@/api/emergency/emergencyDispatch.js"
	import {
		businessTypeList
	} from '@/api/system/businessTypes'
	import uniDataPicker from "../components/uni-data-picker/components/uni-data-picker/uni-data-picker.vue"
	export default {
		name: "pigeonhole",
		components: {
			uniDataPicker
		},
		data() {
			return {
				loading: false,
				fileList: [],
				businessTypeList: [],
				mapObj: {
					text: "name",
					value: "id"
				},
				businessTypeId: null,
				dangerText: "", // 险情原因
				summaryText: "", // 经验总结
				projectName: "",
				allName: "",
				data: {},
			}
		},
		onLoad(option) {
			const eventChannel = this.getOpenerEventChannel();
			eventChannel.on('setData', (data) => {
				console.log("setDatasetDatasetData111111111", data.data)
				this.data = data.data
				this.eventId = data.data.id
				this.getBusinessTypeList()
			})
		},
		methods: {
			dangerTextBlur(e){
				this.dangerTextCursor=e.detail.cursor
			},
			setdangerText(text){
				let index=this.dangerTextCursor||0
				let str=this.dangerText||""
				this.dangerTextCursor+=text.length
				text=str.slice(0, index) + text + str.slice(index);
				this.dangerText=text
			},
			summaryTextBlur(e){
				this.summaryTextCursor=e.detail.cursor
			},
			setsummaryText(text){
				let index=this.summaryTextCursor||0
				let str=this.summaryText||""
				this.summaryTextCursor+=text.length
				text=str.slice(0, index) + text + str.slice(index);
				this.summaryText=text
			},
			getBusinessTypeList() {
				let param = {
					"currPage": 1,
					"pageSize": 10000,
					params: {

					}
				}
				businessTypeList(param).then(data => {
					if (data.success) {
						let list = data.result.list || []
						let newList = []
						for (let item of list) {
							for (let ite of list) {
								if (ite.parentId === item.id) {
									if (!item.children) {
										item.children = []
									}
									item.children.push(ite)
								}
							}
							newList.push(item)
						}
						this.businessTypeList = this.businessTypeList=newList.filter(item=>item.children && item.children.length>0)
						console.log(this.businessTypeList, 'bud')
					}
				})
			},

			onchange(e) {
				console.log(e, 'eeeee', this.businessTypeId)
				const value = e.detail.value
			},

			// 提交
			async submit() {
				if(!this.businessTypeId){
					uni.showToast({
						icon: "none",
						title: "业务类型不能为空"
					})
					return
				}
				if(!this.dangerText){
					uni.showToast({
						icon: "none",
						title: "险情原因不能为空"
					})
					return
				}
				if(!this.summaryText){
					uni.showToast({
						icon: "none",
						title: "经验总结不能为空"
					})
					return
				}
				let _data = {
					// docId: null,
					eventId: +this.eventId,
					summary: this.summaryText,
					reason: this.dangerText,
					state: 1
				}
				let res = await getEventSummarySave(_data)
				if (res.success) {
					let param = {
						id: this.eventId,
						businessTypeId: this.businessTypeId
					}
					let res3 = await getEventSave(param)
					this.loading = false
					if (res3.success) {
						uni.showToast({
							icon: "none",
							title: "保存成功"
						})
						uni.switchTab({
							url:"/pages/homePage/homePage"
						})
					}
					// this.removeActiveEventObj()
				}
			},
			// removeActiveEventObj() {
			// 	let oldData = JSON.parse(uni.getStorageSync("activeEventObj") || "{}")
			// 	let userId = this.$store.getters.userName.id
			// 	delete oldData[userId]
			// 	uni.setStorageSync("activeEventObj", JSON.stringify(oldData))
			// },
		}
	}
</script>

<style lang="scss" scoped>
	.pigeonhole {
		padding-bottom: 100rpx;

		.content {
			background-color: #F5F7FA;
			padding-top: 30rpx;
		}

		.box {
			padding: 0rpx 30rpx;
			background-color: #FFFFFF;
		}

		.item {
			.name {
				padding-top: 40rpx;
				margin-bottom: 20rpx;
				font-size: 28rpx;
				color: #445469;
			}

			.value {
				height: 80rpx;
				opacity: 1;
				border: 2rpx solid #DCDFE6;
				cursor: pointer;
			}
		}

		.upload {
			height: 96rpx;
			line-height: 96rpx;
			background: #F5F7FA;
			font-weight: bold;
			color: #445469;
			text-align: center;
			margin-top: 42rpx;
			cursor: pointer;
		}
	}

		.form-item {
			width: 100%;
			position: relative;
		}

		.form-item-description {
			.form-item-icon {
				width: 48rpx;
				height: 48rpx;
			}

			.form-item-icon-voice {
				position: absolute;
				top: 10rpx;
				right: 16rpx;
				z-index: 3;
			}
			.form-item-icon-clear{
				position: absolute;
				right: 16rpx;
				bottom: 10rpx;
				z-index: 2;
			}
		}
</style>
